//
//  ViewControllerBeizerAni.m
//  核心动画
//
//  Created by chichaojituan521 on 2017/6/1.
//  Copyright © 2017年 linch. All rights reserved.
//
/*
 //根据一个矩形画曲线
 + (UIBezierPath *)bezierPathWithRect:(CGRect)rect
 
 //根据矩形框的内切圆画曲线
 + (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect
 
 //根据矩形画带圆角的曲线
 + (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius
 
 //在矩形中，可以针对四角中的某个角加圆角
 + (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
 参数：
 corners:枚举值，可以选择某个角
 cornerRadii:圆角的大小
 
 //以某个中心点画弧线
 
 + (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
 参数：
 center:弧线中心点的坐标
 radius:弧线所在圆的半径
 startAngle:弧线开始的角度值
 endAngle:弧线结束的角度值
 clockwise:是否顺时针画弧线
 
 //画二元曲线，一般和moveToPoint配合使用
 - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
 参数：
 endPoint:曲线的终点
 controlPoint:画曲线的基准点
 
 //以三个点画一段曲线，一般和moveToPoint配合使用
 - (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
 参数：
 endPoint:曲线的终点
 controlPoint1:画曲线的第一个基准点
 controlPoint2:画曲线的第二个基准点
 */
#import "ViewControllerBeizerAni.h"

@interface ViewControllerBeizerAni ()
{
    UIBezierPath *path;
    CAShapeLayer *pathLayer;
}
@end

@implementation ViewControllerBeizerAni

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSArray *arrName = @[@"移除",@"拆线动画",@"曲线动画",@"画圆动画",@"矩形动画",@"内切圆动画",@"圆角矩形",@"指定圆角",@"五角星",@"内接五角"];
    
    
    for (int i = 0;i<10 ; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        btn.backgroundColor = [UIColor orangeColor];
        float JG = (self.view.frame.size.width - 70*5)/6.0;
        float btnx = JG + (JG + 70)*(i%5);
        float btny = 650 + (JG+30)*(i/5);
        btn.frame = CGRectMake(btnx, btny, 70, 30);
        [self.view addSubview:btn];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:arrName[i] forState:UIControlStateNormal];
    }
    
    
    
    
    
}

-(void)btnClick:(UIButton *)sender
{
    switch (sender.tag) {
        case 0:
            [self removeShapeLayer];
            break;
        case 1:
            [self zheXianDongHua];
            break;
        case 2:
            [self quXianDongHua];
            break;
        case 3:
            [self circleDongHua];
            break;
        case 4:
            [self rectDongHua];
            break;
        case 5:
            [self neiQieYuanDongHua];
            break;
        case 6:
            [self yuanJiaoJuXingDongHua];
            break;
        case 7:
            [self mouGeYuanJiaoJuXingDongHua];
            break;
        case 8:
            [self wuJiaoXing];
            break;
        case 9:
            [self neiQieYuanDongHua];
            [self wuJiaoXing];
            break;
        
            //
        default:
            break;
    }
}
#pragma mark -- 拆线动画
-(void)zheXianDongHua
{
    path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(5.0,70.0)];
    [path addLineToPoint:CGPointMake(120.0, 500.0)];
    [path addLineToPoint:CGPointMake(220, 200)];
    [path addLineToPoint:CGPointMake(310, 440)];
    [path addLineToPoint:CGPointMake(100, 110)];
    [path closePath];
    
    
    pathLayer = [CAShapeLayer layer];
    
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor redColor] CGColor];
    //        pathLayer.fillColor = [UIColor greenColor].CGColor;
    pathLayer.fillColor = nil;//默认填充黑色
    pathLayer.lineWidth = 2.0f;
    pathLayer.lineJoin = kCALineJoinBevel;
    
    [self.view.layer addSublayer:pathLayer];
    
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];

}
#pragma mark -- 曲线动画
-(void)quXianDongHua
{
    path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(200.0,70.0)];
    [path addCurveToPoint:CGPointMake(200.0,300.0) controlPoint1:CGPointMake(50.0,170.0)controlPoint2:CGPointMake(300.0,270.0)];

    
    pathLayer = [CAShapeLayer layer];
    
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor redColor] CGColor];
    //        pathLayer.fillColor = [UIColor greenColor].CGColor;
    pathLayer.fillColor = nil;//默认填充黑色
    pathLayer.lineWidth = 2.0f;
    pathLayer.lineJoin = kCALineJoinBevel;
    
    [self.view.layer addSublayer:pathLayer];
    
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    
}

#pragma mark -- 以某个中心点画弧线
-(void)circleDongHua
{
    path = [UIBezierPath bezierPath];
    
    [path addArcWithCenter:CGPointMake(200, 350) radius:100 startAngle:0.f endAngle:M_PI clockwise:YES];//clockwise :动画方向  yes顺时针  no:逆时针
    
    pathLayer = [CAShapeLayer layer];
    
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor redColor] CGColor];
    //        pathLayer.fillColor = [UIColor greenColor].CGColor;
    pathLayer.fillColor = nil;//默认填充黑色
    pathLayer.lineWidth = 2.0f;
    pathLayer.lineJoin = kCALineJoinBevel;
    
    [self.view.layer addSublayer:pathLayer];
    
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    
}
#pragma mark -- 画矩形动画
-(void)rectDongHua
{
    path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 200)];
    
    
    pathLayer = [CAShapeLayer layer];

    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor redColor] CGColor];
    //        pathLayer.fillColor = [UIColor greenColor].CGColor;
    pathLayer.fillColor = nil;//默认填充黑色
    pathLayer.lineWidth = 2.0f;
    pathLayer.lineJoin = kCALineJoinBevel;
    
    [self.view.layer addSublayer:pathLayer];
    
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    
}
#pragma mark -- 画内切圆动画
-(void)neiQieYuanDongHua
{
    path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 200, 200, 200)];
    
    
    pathLayer = [CAShapeLayer layer];
    
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor redColor] CGColor];
    //        pathLayer.fillColor = [UIColor greenColor].CGColor;
    pathLayer.fillColor = nil;//默认填充黑色
    pathLayer.lineWidth = 4.0f;
    pathLayer.lineJoin = kCALineJoinBevel;
    
    [self.view.layer addSublayer:pathLayer];
    
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    
}
#pragma mark -- 矩形画带圆角的曲线
-(void)yuanJiaoJuXingDongHua
{
    path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 200, 200) cornerRadius:20];
    
    pathLayer = [CAShapeLayer layer];
    
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor redColor] CGColor];
    //        pathLayer.fillColor = [UIColor greenColor].CGColor;
    pathLayer.fillColor = nil;//默认填充黑色
    pathLayer.lineWidth = 2.0f;
    pathLayer.lineJoin = kCALineJoinBevel;
    
    [self.view.layer addSublayer:pathLayer];
    
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    
}

#pragma mark -- 矩形画带圆角的曲线
-(void)mouGeYuanJiaoJuXingDongHua
{
    path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 200, 200) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
    pathLayer = [CAShapeLayer layer];
    
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor redColor] CGColor];
    //        pathLayer.fillColor = [UIColor greenColor].CGColor;
    pathLayer.fillColor = nil;//默认填充黑色
    pathLayer.lineWidth = 2.0f;
    pathLayer.lineJoin = kCALineJoinBevel;
    
    [self.view.layer addSublayer:pathLayer];
    
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    
}
#pragma mark -- 五角星
-(void)wuJiaoXing
{
    //中心点  200,300   半径 100
    CGPoint point1 =  CGPointMake(200, 200);
    CGPoint point2 =  CGPointMake(200 + 100*sin(36/180.0*M_PI), 300 + 100*cos(36/180.0*M_PI));
    CGPoint point3 =  CGPointMake(200 - 100*cos(18/180.0*M_PI), 300 - 100*sin(18/180.0*M_PI));
    CGPoint point4 =  CGPointMake(200 + 100*cos(18/180.0*M_PI), 300 - 100*sin(18/180.0*M_PI));
    CGPoint point5 =  CGPointMake(200 - 100*sin(36/180.0*M_PI), 300 + 100*cos(36/180.0*M_PI));
    
    path = [UIBezierPath bezierPath];
    [path moveToPoint:point1];
    [path addLineToPoint:point2];
    [path addLineToPoint:point3];
    [path addLineToPoint:point4];
    [path addLineToPoint:point5];
    [path closePath];
    
    
    pathLayer = [CAShapeLayer layer];
    
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor redColor] CGColor];
//            pathLayer.fillColor = [UIColor greenColor].CGColor;
    pathLayer.fillColor = nil;//默认填充黑色
    pathLayer.lineWidth = 4.0f;
    pathLayer.lineJoin = kCALineJoinBevel;
    
    [self.view.layer addSublayer:pathLayer];
    
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    //延迟填充颜色
    [self performSelector:@selector(fillWithColor) withObject:nil afterDelay:2.f];
}
//填充颜色
-(void)fillWithColor
{
    pathLayer.fillColor = [UIColor greenColor].CGColor;
}
//移除
-(void)removeShapeLayer
{
    [pathLayer removeFromSuperlayer];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
}


@end
